package cn.liwq.demo.mq.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: demo-common </p>
 * <p>Function:  </p>
 * <p>Description:  </p>
 * <p>Copyright: Copyright(c) 2018 Ucarinc </p>
 * <p>Company: Ucarinc </p>
 *
 * @author weiqiang.li@ucarinc.com
 * @version 1.0
 * @date 2018-08-09 15:37:11
 */
public class ProducerDemo {
    public static void main(String[] args) {
        Properties pro = new Properties();
        pro.put("bootstrap.servers", "127.0.0.1:9092,127.0.0.1:9093");
        pro.put("acks", "all");
        pro.put("retries", 0);
        pro.put("batch.size", 16384);
        pro.put("linger.ms", 1);
        pro.put("buffer.memory", 1024*1024);
        pro.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        pro.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = null;
        try {
            producer = new KafkaProducer<String, String>(pro);
            for (int i = 0; i < 100; i++) {
                String msg = "msg-" + i;
                producer.send(new ProducerRecord<String, String>("liwq-r-topic", msg));
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.close();
        }
    }
}
